package cn.bearspark.happening.provider.rpc;

import cn.bearspark.happening.dto.happening.ForwardReqDTO;
import cn.bearspark.happening.dto.vote.VoteDetailDTO;
import cn.bearspark.happening.dto.vote.VoteReqDTO;
import cn.bearspark.happening.enumeration.vote.SynchronousEnum;
import cn.bearspark.happening.provider.service.HappeningService;
import cn.bearspark.happening.provider.service.VoteService;
import cn.bearspark.happening.rpc.VoteRpc;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

/**
 * @author f
 */
@Component
@DubboService
public class VoteRpcImpl implements VoteRpc {

    private final VoteService voteService;
    private final HappeningService happeningService;

    public VoteRpcImpl(VoteService voteService,
                       HappeningService happeningService) {
        this.voteService = voteService;
        this.happeningService = happeningService;
    }

    @Override
    public void vote(VoteReqDTO voteReqDTO) {
        String optionsInfo = voteService.vote(voteReqDTO);

        // 如果不需要将投票同步到动态，则直接返回
        if (voteReqDTO.getSynchronous() == SynchronousEnum.NO_SYNC.getCode()) {
            return;
        }

        ForwardReqDTO forwardReqDTO = new ForwardReqDTO();
        // 将投票分享到动态一定是第一次转发动态，所以一定被记录了
        forwardReqDTO.setRecorded(true);
        forwardReqDTO.setUserId(voteReqDTO.getVoterId());
        forwardReqDTO.setQuotedHappeningId(voteReqDTO.getVoteId());
        forwardReqDTO.setContent("我投给了" + optionsInfo);
        happeningService.forward(forwardReqDTO);
    }

    @Override
    public VoteDetailDTO detail(long voteId, long userId) {
        return voteService.detail(voteId, userId);
    }
}
